﻿using Polly;
using System;
namespace AspNetCore.HttpClientAndPolly.PollyAttribute
{
    /// <summary>
    /// timeout
    /// </summary>
    public class CustomPollyTimeOutAttribute : PollyRetryAttribute
    {
        public override Action<ISyncPolicy> Do(Action<ISyncPolicy> action)
        {
            Policy timeoutPolicy = Policy.Timeout(TimeSpan.FromMinutes(1), Polly.Timeout.TimeoutStrategy.Optimistic, (context, timespan, task, exception) =>
            {
                Console.WriteLine($"Message:{exception.Message} Count:{context.Count} TotalSeconds:{timespan.TotalSeconds}");
            });
            return s =>
            {
                Policy policy = null;
                if (s == null) policy = timeoutPolicy;
                else policy = Policy.Wrap(s, timeoutPolicy);
                action(policy);
            };
        }
    }
}
